using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;
using SigmaStock_Components;

namespace project
{
    public partial class Magazine : Form
    {
        string query;
        OleDbConnection conn;
        public static string magname = "";
        public Magazine()
        {
            
            InitializeComponent();
            textBox1.ReadOnly = false;
            dbconnection();
        }

        public string a
        {
            set
            {
                magname = value;
            }
        }

        private void UpdateDataGrid()
        {
            String s;
                s = "Select * from magazine";
                OleDbCommand cmd1 = new OleDbCommand(s, conn);
                OleDbDataAdapter da = new OleDbDataAdapter(s, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "magazine");
                magazineDataGridView.DataSource = ds;
                magazineDataGridView.DataMember = "magazine";
                magazineBindingSource.ResetBindings(false);
                this.Refresh();
        }



        private void dbconnection()
        {
            conn = DBComponent.Mag_DealerDB.dbconnection();
            conn.Open();

        }

        private bool InsertValidation()
        {

            query = "select MNAME from magazine;";
            List<string> mag_list = new List<string>();
            OleDbCommand command = new OleDbCommand(query, conn);
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                mag_list.Add(reader[0].ToString());
            }

            if (mag_list.Contains(textBox1.Text))
            {
                textBox4.Visible = true;
                MessageBox.Show("Magazine already exists in database !!!");
                return false;
            }
            return true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ValidateMagazineData() && InsertValidation())
            {

                String mname = (String)textBox1.Text;
                //     DateTime mdate = (DateTime)dateTimePicker1.Value;
                int price = int.Parse(textBox3.Text);
                int act_price = int.Parse(textBox2.Text);
                String publisher = (String)comboBox1.Text;
                String frequency = (String)comboBox2.Text;
                try
                {
                    Console.WriteLine("Entered button1_click");
                    string sqlquery = "insert into magazine values('" + mname + "','" + price + "','" + act_price + "','" + publisher + "','" + frequency + "');";
                    OleDbCommand cmd = new OleDbCommand(sqlquery, conn);

                    int res = cmd.ExecuteNonQuery();
                    Console.WriteLine(res);
                    Program.InvokePar(this, "SigmaStock_Components.Magazine_Retailer", mname, act_price);
                    MessageBox.Show("Data Inserted Successfully !!!");
                    UpdateDataGrid();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }

        }

        private void magazineBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.magazineBindingSource.EndEdit();
            this.magazineTableAdapter.Update(this.mag_dealerDataSet.magazine);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'mag_dealerDataSet.magazine' table. You can move, or remove it, as needed.
            this.magazineTableAdapter.Fill(this.mag_dealerDataSet.magazine);
            dbconnection();
            string query = "select PNAME from publisher;";
            OleDbCommand command = new OleDbCommand(query, conn);
            OleDbDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                comboBox1.Items.Add(reader[0].ToString());
            }
            //comboBox1.Items();
            reader.Close();

        }

 
        public void UpdateMagazine()
        {
            int flag = 0;
            //MessageBox.Show(magname);
            String query = "select * from magazine where MNAME= '" + magname + "';";

            OleDbCommand command = new OleDbCommand(query, conn);
            OleDbDataReader reader = command.ExecuteReader();

            while (reader.Read())
                flag = 1;
            if (flag == 1)
                MessageBox.Show("Magazine record deleted !!! ");
            else
                MessageBox.Show("Magazine record doesn't exist");
            reader.Close();

            String query1 = "delete MNAME from magazine where MNAME='" + magname + "';";

            OleDbCommand cmd = new OleDbCommand(query1, conn);
            cmd.ExecuteNonQuery();
            UpdateDataGrid();
        }

       

        private bool ValidateMagazineData()
        {
            if (String.IsNullOrEmpty(textBox1.Text)||(!(Validation.IsAlphaNumeric(textBox1.Text))))
            {
                textBox4.Visible = true;
                Console.Beep();
                textBox4.Text = "Check Magazine name";
                return false;
            }

            if (String.IsNullOrEmpty(textBox2.Text) || (!(Validation.IsPositiveNumber(textBox2.Text))))
            {
                textBox4.Visible = true;
                Console.Beep();
                textBox4.Text = "Check Magazine actual price";
                return false;
            }
            if (String.IsNullOrEmpty(textBox3.Text) || (!(Validation.IsPositiveNumber(textBox3.Text))))
            {
                textBox4.Visible = true;
                Console.Beep();
                textBox4.Text = "Check Magazine price";
                return false;
            }
            if (comboBox1.SelectedItem == null)
            {
                textBox4.Visible = true;
                Console.Beep();
                textBox4.Text = "Check publisher";
                return false;
            }

            if (comboBox2.SelectedItem == null)
            {
                textBox4.Visible = true;
                Console.Beep();
                textBox4.Text = "Check frequency";
                return false;
            }

            return true;

        }

        private void magazineDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                //Console.WriteLine("Reached method");
                DataGridViewRowCollection datarows = magazineDataGridView.Rows;
                DataGridViewRow selrow = datarows[e.RowIndex];
                selrow.Selected = true;
                foreach (DataGridViewRow dr in datarows)
                {
                    if (dr.Selected)
                    {
                        //Console.WriteLine("Reached row");
                        //Console.WriteLine(dr.Cells[1].Value);
                        textBox1.ReadOnly = true;
                        textBox1.Text = dr.Cells[0].Value.ToString();
                        // dateTimePicker1.Value = (DateTime)dr.Cells[1].Value;
                        textBox3.Text = dr.Cells[1].Value.ToString();
                        textBox2.Text = dr.Cells[2].Value.ToString();
                        comboBox1.Text = dr.Cells[3].Value.ToString();
                        comboBox2.Text = dr.Cells[4].Value.ToString();
                    }

                }
            }
            catch (Exception ex)
            {

            }


        }

 

        private void Magazine_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (Application.OpenForms.Count == 1)
            {
                Program.main_form.Show();
            }
        }

        private void Magazine_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'mag_dealerDataSet.magazine' table. You can move, or remove it, as needed.
    
            string query = "select PNAME from publisher;";
            OleDbCommand command = new OleDbCommand(query, conn);
            OleDbDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                comboBox1.Items.Add(reader[0].ToString());
            }
            //comboBox1.Items();
            reader.Close();
            this.magazineTableAdapter.Fill(this.mag_dealerDataSet.magazine);


        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.ReadOnly = false;
            textBox1.ResetText();
            textBox2.ResetText();
            textBox3.ResetText();
            comboBox1.ResetText();
            comboBox2.ResetText();
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            if (ValidateMagazineData())
            {
                String mname = (String)textBox1.Text;
                //   DateTime mdate = (DateTime)dateTimePicker1.Value;
                int act_price = int.Parse(textBox2.Text);
                int price = int.Parse(textBox3.Text);
                String publisher = (String)comboBox1.Text;
                String frequency = (String)comboBox2.Text;
                try
                {
                    string sqlquery = "update magazine set ACTUAL_PRICE='" + act_price + "',PRICE='" + price + "',PNAME='" + publisher + "',FREQUENCY='" + frequency + "' where MNAME='" + mname + "';";
                    OleDbCommand cmd = new OleDbCommand(sqlquery, conn);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Data Modified Successfully !!!");
                    UpdateDataGrid();
                    //   Console.WriteLine(res);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            Program.InvokeModalNoClose(this, "project.Form2");
            MessageBox.Show("Data Deleted Successfully !!!");
            UpdateMagazine();
        }


  

    }
}